package cn.rulian.one.file.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.rulian.base.auth.bo.User;
import cn.rulian.base.common.dto.UserInfo;
import cn.rulian.base.common.service.BaseService;
import cn.rulian.base.common.util.xx;
import cn.rulian.base.common.vo.Result;
import cn.rulian.one.file.bo.FileSort_;
import cn.rulian.one.file.bo.SharedFile;
import cn.rulian.one.file.bo.SharedFile_;
import cn.rulian.one.file.dao.SharedFileDao;
import cn.rulian.one.file.vo.SharedFileVo;

/**
 * 文件管理Service
 */
@Service
@Transactional(readOnly = true)
public class SharedFileService extends BaseService<SharedFile>
{

	// private final Logger log = LoggerFactory.getLogger(getClass());

	@Autowired
	EntityManager em;

	@Autowired
	private SharedFileDao dao;

	@Override
	protected SharedFileDao getDao()
	{
		return dao;
	}
		
	/**
	 * 查询
	 */
	public Result query(final HttpSession session,final SharedFileVo vo)
	{
		Sort sort = new Sort("desc".equals(vo.getOrder())?Direction.DESC:Direction.ASC, vo.getSort()); // 排序
		Pageable pageable = new PageRequest(vo.getPage() - 1, vo.getRows(), sort); // 分页查询条件

		//动态查询对象
		Specification<SharedFile> specification = new Specification<SharedFile>()
		{
			@Override
			public Predicate toPredicate(Root<SharedFile> root, CriteriaQuery<?> cq, CriteriaBuilder cb)
			{
				return buildPredicate(session,root, cb, vo);
			}
		};

		// 查询
		Page<SharedFile> page = dao.findAll(specification, pageable);
		Result result = Result.getResult(page);


		return result;
	}
	

	/**
	 * 构建查询条件
	 */
	private Predicate buildPredicate(HttpSession session,Root<SharedFile> root, CriteriaBuilder cb, SharedFileVo vo)
	{
		List<Predicate> predicates = new ArrayList<Predicate>();

	
		// 类别 ID
		String fileSortId = vo.getFileSortId();	
		if(!xx.isEmpty(fileSortId))
		{
			long num = xx.toLong(fileSortId);
			predicates.add(cb.equal(root.get(SharedFile_.fileSort).get(FileSort_.id), num));
		}


		//文件名称
		String showName = vo.getShowName();
		if(!xx.isEmpty(showName))
		{
			predicates.add(cb.like(root.get(SharedFile_.showName), "%" + showName + "%"));
		}


		// 上传时间
		String addTime_start = vo.getAddTime_start();	//开始时间
		String addTime_end = vo.getAddTime_end();	//结束时间
		if(!xx.isEmpty(addTime_start))
		{
			Date date = xx.toDate(addTime_start);
			predicates.add(cb.greaterThanOrEqualTo(root.get(SharedFile_.addTime), date));

		}

		// 结束时间
		if(!xx.isEmpty(addTime_end))
		{
			Date date = xx.toTime(addTime_end + " 23:59:59");
			predicates.add(cb.lessThanOrEqualTo(root.get(SharedFile_.addTime), date));
		}


		//文件类型
		String fileType = vo.getFileType();
		if(!xx.isEmpty(fileType))
		{
			predicates.add(cb.like(root.get(SharedFile_.fileType), "%" + fileType + "%"));
		}
		if(SharedFile.TYPE_MY.equals(fileType))
		{//比对用户
			UserInfo userInfo = UserInfo.getUserInfo(session);
			User user = userInfo.getUser();
			predicates.add(cb.equal(root.get(SharedFile_.userId), user.getId()));
		}


		return cb.and(predicates.toArray(new Predicate[predicates.size()]));
	}

	
	/**
	 * 查询 Excel数据（不分页）
	 */
	public List<SharedFile> QueryExcelData(final SharedFileVo vo)
	{
		/*
		Sort sort = new Sort("desc".equals(vo.getOrder())?Direction.DESC:Direction.ASC, vo.getSort()); // 排序
		//动态查询对象
		Specification<SharedFile> specification = new Specification<SharedFile>()
		{
			@Override
			public Predicate toPredicate(Root<SharedFile> root, CriteriaQuery<?> cq, CriteriaBuilder cb)
			{
				return buildPredicate(root, cb, vo);
			}
		};

		// 查询
		List<SharedFile> list = dao.findAll(specification, sort);
		return list;
		*/
		return null;
	}


}
